const db = require('../db/index')
var dayjs = require('dayjs')
// 获取预约
exports.subscribeList = (req, res) => {
    let data = req.body
    const sql = `select * from subscribe where userId=? and appid=? ORDER BY create_time DESC;`
    db.query(sql, [data.id, data.appid], (err, res2) => {
        if (err) return res.cc(err)
        let list = JSON.parse(JSON.stringify(res2))
        list.forEach((item, index) => {
            const sqlStore = `select * from store where _id=? and appid=?`
            db.query(sqlStore, [item.storeId, data.appid], (err, res3) => {
                if (err) return res.cc(err)
                item.storeInfo = res3[0]
            })
            const sqlMyCar = `select * from mycar where _id=? and appid=?`
            db.query(sqlMyCar, [item.myCarId, data.appid], (err, res4) => {
                if (err) return res.cc(err)
                item.myCarInfo = res4[0]
            })
        })
        setTimeout(() => {
            res.send({
                res: list
            })
        }, 100)
    })
}
// 添加预约
exports.subscribeadd = (req, res) => {
    let data = req.body
    data.state = 0

    const sql = `insert into subscribe set ?`
    db.query(sql, data, (err, res2) => {
        if (err) return res.cc(err)
        res.send({
            message: '添加成功'
        })
    })
}
// 获取预约详情
exports.subscribedetils = (req, res) => {
    const id = req.body.id
    const sql = `select * from subscribe where _id= ?`
    db.query(sql, id, (err, res2) => {
        let list = JSON.parse(JSON.stringify(res2))
        if (err) return res.cc(err)

        list.forEach((item, index) => {
            const sqlStore = `select * from store where _id=?`
            db.query(sqlStore, [item.storeId], (err, res3) => {
                if (err) return res.cc(err)
                delete res3[0].password
                res3[0].location = JSON.parse(res3[0].location)
                item.storeInfo = res3[0]
            })
            const sqlMyCar = `select * from mycar where _id=?`
            db.query(sqlMyCar, [item.myCarId], (err, res4) => {
                if (err) return res.cc(err)
                item.myCarInfo = res4[0]
            })
            const sqlUser = `select * from users where _id=?`
            db.query(sqlUser, [item.userId], (err, res4) => {
                if (err) return res.cc(err)
                item.userInfo = res4[0]
            })
            const sqlFilme = `select * from film where _id=?`
            db.query(sqlFilme, [item.filmId], (err, res4) => {
                if (err) return res.cc(err)
                item.filmInfo = res4[0]
            })
        })
        setTimeout(() => {
            res.send({
                res: list[0]
            })
        }, 100)
    })
}
// 修改状态
exports.subscribeCahngeState = (req, res) => {
    let data = req.body

    const sql = `update subscribe set state=? where _id=?`
    db.query(sql, [data.state, data.id], (err, res2) => {
        if (err) return res.cc(err)
        res.send({
            message: '修改成功'
        })
    })

}
// 根据条件获取预约详情小程序
exports.subscribedetilsSmall = (req, res) => {
    const data = req.body
    let sqlUserInfo = `select * from users where _id=?`
    let sqlStore = `select * from store where _id=?`
    let sqlFilm = `select * from film where _id=?`
    let sqlCar = `select * from mycar where _id=?`
    let userInfo = {}
    let storeInfo = {}
    let filmInfo = {}
    let carInfo = {}
    try {
        db.query(sqlUserInfo, [data.userId], (err, res2) => {
            if (err) return res.cc(err)
            userInfo = res2[0]
        })
        db.query(sqlStore, [data.storeId], (err, res2) => {
            if (err) return res.cc(err)
            storeInfo = res2[0]
        })
        db.query(sqlFilm, [data.filmId], (err, res2) => {
            if (err) return res.cc(err)
            filmInfo = res2[0]
        })
        db.query(sqlCar, [data.carId], (err, res2) => {
            if (err) return res.cc(err)
            carInfo = res2[0]
        })
        setTimeout(() => {
            res.send({
                res: {
                    userInfo,
                    storeInfo,
                    filmInfo,
                    carInfo
                }
            })
        }, 100)
    } catch (error) {
        res.send({
            message: '出错了'
        })
    }


}
// 获取门店预约
exports.subscribeListStore = (req, res) => {
    let data = req.body
    let page = data.page * 1 - 1
    let size = data.size * 1
    let from = page * size
    const sql = `select * from subscribe where storeId=? and appid=? limit ${size} OFFSET  ${from}`
    db.query(sql, [data.storeId, data.appid], (err, res2) => {
        if (err) return res.cc(err)
        let list = JSON.parse(JSON.stringify(res2))
        list.forEach((item, index) => {
            const sqlStore = `select * from store where _id=? and appid=?`
            db.query(sqlStore, [item.storeId, data.appid], (err, res3) => {
                if (err) return res.cc(err)
                item.storeInfo = res3[0]
            })
            const sqlMyCar = `select * from mycar where _id=? and appid=?`
            db.query(sqlMyCar, [item.myCarId, data.appid], (err, res4) => {
                if (err) return res.cc(err)
                item.myCarInfo = res4[0]
            })
            const sqlUser = `select * from users where _id=? and appid=?`
            db.query(sqlUser, [item.userId, data.appid], (err, res4) => {
                if (err) return res.cc(err)
                item.userInfo = res4[0]
            })
        })
        setTimeout(() => {
            res.send({
                res: list
            })
        }, 100)
    })
}